package com.neuedu.studentmanage.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.studentmanage.entity.Pager;
import com.neuedu.studentmanage.entity.Position;
import com.neuedu.studentmanage.entity.Result;
import com.neuedu.studentmanage.entity.Student;
import com.neuedu.studentmanage.service.PositionService;

@Controller
public class PositionController {

	@Autowired
	private PositionService positionService;

	@RequestMapping("position/list")
	@ResponseBody
	public List<Position> positionList() {
		return positionService.findAll();
	}

	@GetMapping("position/index") // 浏览器页面访问地址---浏览器
	public String positionIndex() {
		return "admin/position";// 访问页内地址----程序内部
	}

	@PostMapping("position/index")
	@ResponseBody
	public Pager<Position> positionList(@RequestParam(value = "page", defaultValue = "1")

	Integer page, @RequestParam(value = "rows", defaultValue = "5") Integer rows) {
		Pager<Position> pager = new Pager<>(page, rows);
		List<Position> positionList = positionService.findListByPager(pager);
		Integer total = positionService.findTotalByPager(pager);

		pager.setRows(positionList);
		pager.setTotal(total);// 把total存进来

		return pager;
	}

	@RequestMapping("position/add")
	@ResponseBody
	public Result doAdd(Position position,Integer stuId) {
		System.out.println(stuId);
		System.out.println(position.toString());
		
		Student student = new Student();
		student.setStuId(stuId);
		position.setStudent(student);
		
		Result result = positionService.add(position);
		return result;
	}

	@RequestMapping("position/edit")
	@ResponseBody
	public Result doEdit(Position position) {
		Result result = positionService.edit(position);
		return result;
	}

	@RequestMapping("position/delete")
	@ResponseBody // admin接收传参
	public Result doDelete(Integer goId) {
		List<Position> list = new ArrayList<Position>();
		list.add(new Position(goId, null, null, null, null));

		Result result = positionService.deleteByIds(list);
		return result;
	}

}
